<?php

use Biz\service\Share;
use Biz\service\wikiRanking\Ranking;
use Biz\service\wikiRanking\FullCateRanking;
use Doraemon\service\statSign\Basic as statSign;
use Doraemon\tools\ActionLog;
use Biz\service\brand\Brand as BrandService;

class BrandGloryAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {

        $brand_id = $this->getRequest()->getPost('brand_id', '');
        $act_from = $this->getRequest()->getPost('act_from', '');
        if (!$brand_id) {
            //  throw new Exception('brand_id不能为空');
        }


        $rank_info = Ranking::getBrandGroryByBrandId($brand_id);
        //下线全分类榜单
        /*$top_rank_list = FullCateRanking::getBrandGroryByBrandId($brand_id);
        $top_rank_info = [];
        if ($top_rank_list && is_array($top_rank_list)) {
            $top_rank_info['ranking_name'] = "精选榜单";
            foreach ($top_rank_list as &$val) {
                $statSign = statSign::genStatSign(statSign::MODULE_WIKI_RANKING_BRAND_GLORY, [
                    statSign::EXTEND_KEY_OBJ_ID => $val['ranking_id'],
                ], ['uid' => $this->uid, 'ranking_id' => $val['ranking_id'], 'brand_id' => $brand_id]);
                $val['statSign'] = $statSign;
            }
            $top_rank_info['ranking_list'] = $top_rank_list;
        }*/
        foreach ($rank_info as &$row) {
            $statSign = statSign::genStatSign(statSign::MODULE_WIKI_RANKING_BRAND_GLORY, [
                statSign::EXTEND_KEY_OBJ_ID => $row['ranking_id'],
            ], ['uid' => $this->uid, 'ranking_id' => $row['ranking_id'], 'brand_id' => $brand_id]);
            $row['statSign'] = $statSign;
        }
        $brandUserInfos = BrandService::getBrandUserInfos([$brand_id], true, false, true);
        $RankingDay = Ranking::getRankingDay();
        $day = date("Y.m.d", strtotime($RankingDay));
        $brandData = (isset($brandUserInfos[$brand_id]) && isset($brandUserInfos[$brand_id]['user_info']['brand'])) ? $brandUserInfos[$brand_id]['user_info']['brand'] : [];
        $fans = (isset($brandUserInfos[$brand_id]) && isset($brandUserInfos[$brand_id]['counter'])) ? $brandUserInfos[$brand_id]['counter']['fans'] : 0;
        $result = [
            'brand' => $brandUserInfos[$brand_id],
            //'top_ranking_info' => $top_rank_info,
            'ranking_info' => $rank_info,
            'update_time' => "榜单更新时间: " . $day . "（每周三）",
            'share_info' => Share::getBrandGloryShare($brand_id, $brandData, $fans),
        ];

        ActionLog::log($this->uid, ['act_from' => $act_from, 'brand_id' => $brand_id],
            ActionLog::BRAND_GLORY);
        $this->setSuccess($result);
    }
}